package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.TermClassPrice;
import com.example.result.Result;
import com.example.service.TermClassPriceService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 学期班级学费控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class TermClassPriceController {

    @Autowired
    TermClassPriceService termClassPriceService;

    // ===============================学期班级学费管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices", method = RequestMethod.POST)
    public Result addTermClassPrice(@RequestBody String json) {
        // 将JSON数据转换为TermClassPrice对象实体
        JSONObject parseObject = JSON.parseObject(json);
        TermClassPrice termClassPrice = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), TermClassPrice.class);

        return termClassPriceService.addTermClassPrice(termClassPrice);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices", method = RequestMethod.PUT)
    public Result updateTermClassPrice(@RequestBody String json) {
        // 将JSON数据转换为TermClassPrice对象实体
        JSONObject parseObject = JSON.parseObject(json);
        TermClassPrice termClassPrice = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), TermClassPrice.class);
        return termClassPriceService.updateTermClassPrice(termClassPrice);

    }

    /**
     * @param askId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据学期班级学费编号删除学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices/{Id}", method = RequestMethod.DELETE)
    public Result deleteTermClassPriceById(@PathVariable("Id") String Id) {
        return termClassPriceService.deleteTermClassPriceById(Id);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices", method = RequestMethod.GET)
    public Result queryAllTermClassPrice() {
        return termClassPriceService.queryAllTermClassPrice();

    }

    /**
     * @param Id
     * @return 以JSON格式返回该学期班级学费信息
     * @describe 根据编号查询学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices/{Id}", method = RequestMethod.GET)
    public Result queryTermClassPriceById(@PathVariable("Id") String Id) {
        return termClassPriceService.queryTermClassPriceById(Id);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices/excels", method = RequestMethod.POST)
    public Result addTermClassPriceByExcel(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        List<TermClassPrice> list = JSON.parseArray(parseObject.getString("json"), TermClassPrice.class);

        return termClassPriceService.addTermClassPriceByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学期班级学费编号批量删除学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices/dels", method = RequestMethod.POST)
    public Result deleteTermClassPriceByIds(@RequestBody Map<String, String> Ids) {

        return termClassPriceService.deleteTermClassPriceByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 学期班级学费信息模糊查询
     */
    @RequestMapping(value = "/termClassPrices/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return termClassPriceService.serachByInfo(info);

    }


    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学期班级学费信息
     */
    @RequestMapping(value = "/termClassPrices/termClassPriceInfos", method = RequestMethod.GET)
    public Result queryAllTermClassPriceInfo() {
        return termClassPriceService.queryAllTermClassPriceInfo();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有无价格学期和班级信息
     */
    @RequestMapping(value = "/termClassPrices/termClassInfos", method = RequestMethod.GET)
    public Result queryAllTermClassInfo() {
        return termClassPriceService.queryAllTermClassInfo();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学期和班级信息
     */
    @RequestMapping(value = "/termClassPrices/termClassNameInfos", method = RequestMethod.GET)
    public Result queryAllTermClassNameInfo() {
        return termClassPriceService.queryAllTermClassNameInfo();
    }
}
